package com.geticsen.leetcode;

import java.util.HashMap;
import java.util.Map;
//https://leetcode-cn.com/problems/house-robber-iii/
public class Solution_337 {
    private Map<TreeNode, Integer> cache = new HashMap<>();

    public int rob(TreeNode root) {
        if (root == null) return 0;
        if (cache.get(root) != null) {
            return cache.get(root);
        }
        int robCur = 0;
        if (root.left != null) {
            robCur += (rob(root.left.left) + rob(root.left.right));
        }
        if (root.right != null) {
            robCur += (rob(root.right.left) + rob(root.right.right));
        }
        robCur += root.val;
        int notRobCur = rob(root.right) + rob(root.left);
        cache.put(root, Math.max(robCur, notRobCur));
        return Math.max(robCur, notRobCur);
    }
}
